package com.sunrate.investingquote.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author yyh
 * @description:
 * @date 2023/06/15/ 16:32
 */
@Configuration
public class AsynConfig {


    @Bean("schedulerThreadPool")
    public ThreadPoolTaskScheduler schedulerThreadPool() {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        // 线程池大小
        threadPoolTaskScheduler.setPoolSize(10);
        // 线程名称
        threadPoolTaskScheduler.setThreadNamePrefix("schedulerThreadPool-");
        // 等待时长
        threadPoolTaskScheduler.setAwaitTerminationSeconds(30);
        threadPoolTaskScheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        // 调度器shutdown被调用时等待当前被调度的任务完成
        threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
        threadPoolTaskScheduler.initialize();
        return threadPoolTaskScheduler;
    }

    @Bean("serviceThreadPool")
    public ThreadPoolTaskExecutor serviceThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //配置核心线程数
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        //线程名称
        executor.setThreadNamePrefix("serviceThreadPool-");
        //配置队列大小
        executor.setQueueCapacity(200);
        //空闲存活时间
        executor.setKeepAliveSeconds(50);
        executor.setDaemon(true);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        return executor;
    }
}
